/**
 * 初始化详情对话框
 */
var PolicyInfoDlg = {
    policyInfoData : {},
    canSubmit : true,
    validateFields: {
        insuranceId: {
            validators: {
                notEmpty: {
                    message: '保险公司不能为空'
                }
            }
        },
        effectiveDate: {
            validators: {
                notEmpty: {
                    message: '生效日期不能为空'
                },
                callback: {
                    message: '生效日期需早于失效日期',
                    callback: function() {
                        return $("#effectiveDate").val() <= $("#invalidDate").val();
                    }
                }
            }
        },
        policyType: {
            validators: {
                notEmpty: {
                    message: '保单类型不能为空'
                }
            }
        },
        tenantId: {
            validators: {
                notEmpty: {
                    message: '所属客户不能为空'
                }
            }
        },
        policyCode: {
            validators: {
                notEmpty: {
                    message: '保单号不能为空'
                }
            }
        },
        invalidDate: {
            validators: {
                notEmpty: {
                    message: '失效日期不能为空'
                }
            }
        },
        paymentCycle: {
            validators: {
                callback: {
                    message: '请选择缴费周期',
                    callback: function() {
                        return $("#paymentCycle").val() >= 1 &&  $("#paymentCycle").val() <= 3;
                    }
                }
            }
        },
        chargeMode: {
            validators: {
                callback: {
                    message: '请选择计费方式',
                    callback: function() {
                        return $("#paymentCycle").val() == 1 ||  $("#paymentCycle").val() == 2;
                    }
                }
            }
        },
        deadline: {
            validators: {
                callback: {
                    message: '请选择计费方式',
                    callback: function() {
                        if($("#paymentCycle").val() == 2 && $("#paymentCycle").val() == 2){
                            return $("#deadline").val() >= 1 &&  $("#deadline").val() <= 28;
                        }
                        return $("#deadline").val() == 0;
                    }
                }
            }
        },
    }
};

/**
 * 清除数据
 */
PolicyInfoDlg.clearData = function() {
    this.policyInfoData = {};
}
/**
 * 选择缴费周期
 */
PolicyInfoDlg.selectPaymentCycle = function() {
    var status = $("#paymentCycle").val();
    $("#deadline").val('0');
    $("#chargeMode").val('0');
    if(status <= 0){
        $("#chargeMode option").removeAttr("disabled");
        $("#chargeMode").attr("disabled","true");
        $("#deadline").attr("disabled","true");
    }else{
        //取消计费方式不可选
        $("#chargeMode option").removeAttr("disabled");
        $("#chargeMode").removeAttr("disabled");
        if(status ==2){
            $("#chargeMode option[value='1']").attr("disabled","true");
        }else{
            $("#chargeMode option[value='2']").attr("disabled","true");
            $("#deadline").attr("disabled","true");
        }
    }
}
/**
 * 选择计费方式
 */
PolicyInfoDlg.selectChargeMode = function() {
    var status = $("#chargeMode").val();
    $("#deadline").val('0');
    if(status < 2){
        $("#deadline").attr("disabled","true");
    }else{
        $("#deadline").removeAttr("disabled");
    }
}
/**
 * 选择保单类型
 */
PolicyInfoDlg.selectPolicyType = function() {
    var status = $("#policyType").val();
    $("#tenantId").val($("#tenantIdValue").val());
    if(status == 1){
        $("#tenantId").val("");
        $("#tenantId").attr("disabled","true");
    }else{
        $("#tenantId").removeAttr("disabled");
    }
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PolicyInfoDlg.set = function(key, val) {
    this.policyInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PolicyInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
PolicyInfoDlg.close = function() {
    parent.layer.close(window.parent.Policy.layerIndex);
}

/**
 * 收集数据
 */
PolicyInfoDlg.collectData = function() {
    this
    .set('id')
    .set('insuranceId')
    .set('effectiveDate')
    .set('invalidDate')
    .set('policyType')
    .set('policyCode')
    .set('paymentCycle')
    .set('deadline')
    .set('chargeMode')
    .set('tenantId')
    .set('comment');
}

/**
 * 验证数据是否为空
 */
PolicyInfoDlg.validate = function () {
    $('#policyInfoForm').data("bootstrapValidator").resetForm();
    $('#policyInfoForm').bootstrapValidator('validate');
    var result = $("#policyInfoForm").data('bootstrapValidator').isValid();
    if(result){
        var paymentCycle = this.get("paymentCycle");
        var chargeMode = this.get("chargeMode");
        var deadline = this.get("deadline");
        if(paymentCycle==0){
            Feng.alert("请选择缴费周期!");
            return false;
        }
        if(chargeMode==0){
            Feng.alert("请选择计费方式!");
            return false;
        }
        if(paymentCycle==2 && chargeMode==2 && deadline==0){
            Feng.alert("请选择截至日!");
            return false;
        }
    }
    return result;
};

/**
 * 提交添加
 */
PolicyInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    if (!this.canSubmit) {
        return;
    }
    this.canSubmit = false;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/policy/add", function(data){
        Feng.success("添加成功!");
        window.parent.Policy.table.refresh();
        PolicyInfoDlg.canSubmit = true;
        PolicyInfoDlg.close();
    },function(data){
        setTimeout(function () {
            PolicyInfoDlg.canSubmit = true;
        },3000);
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.policyInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
PolicyInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    if (!this.canSubmit) {
        return;
    }
    this.canSubmit = false;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/policy/update", function(data){
        Feng.success("修改成功!");
        window.parent.Policy.table.refresh();
        PolicyInfoDlg.canSubmit = true;
        PolicyInfoDlg.close();
    },function(data){
        setTimeout(function () {
            PolicyInfoDlg.canSubmit = true;
        },3000);
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.policyInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("policyInfoForm", PolicyInfoDlg.validateFields);
    //初始化保险公司、保单类型、所属客户
    $("#insuranceId").val($("#insuranceIdValue").val());
    $("#policyType").val($("#policyTypeValue").val());
    //选择保单类型
    PolicyInfoDlg.selectPolicyType();
    $("#tenantId").val($("#tenantIdValue").val());
    $("#paymentCycle").val($("#paymentCycleValue").val());
    //选择缴费周期
    PolicyInfoDlg.selectPaymentCycle();
    $("#chargeMode").val($("#chargeModeValue").val());
    //选择计费方式
    PolicyInfoDlg.selectChargeMode();
    $("#deadline").val($("#deadlineValue").val());
});
